package com.buddies.lottery.common;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.buddies.lottery.App;
import com.buddies.lottery.data.bean.User;
import com.buddies.lottery.data.bean.Wallet;
import com.google.gson.reflect.TypeToken;
import com.tencent.bugly.Bugly;

import java.util.List;

public class ConfigManager {

    private static ConfigManager mInstance;

    private ConfigManager() {

    }

    public static synchronized ConfigManager getInstance() {
        if (mInstance == null) {
            mInstance = new ConfigManager();
        }
        return mInstance;
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(getUserId()) && !TextUtils.isEmpty(getToken());
    }

    public void setId(long id) {
        SPUtils.getInstance().put("id", id);
    }

    public void setUserId(String userId) {
        SPUtils.getInstance().put("user_id", userId);

        Bugly.setUserId(App.getInstance().getContext(), ConfigManager.getInstance().getUserId());
    }

    public void setToken(String token) {
        SPUtils.getInstance().put("token", token);
    }

    public long getId() {
        return SPUtils.getInstance().getLong("id", 0);
    }

    public String getUserId() {
        return SPUtils.getInstance().getString("user_id", "");
    }

    public String getToken() {
        return SPUtils.getInstance().getString("token", "");
    }

    public void setUserType(int userType) {
        SPUtils.getInstance().put("user_type", userType);
    }

    public int getUserType() {
        return SPUtils.getInstance().getInt("user_type", 0);
    }

    public boolean isAgent() {
        return getUserType() == 1;
    }

    public void setRebate(float rebate) {
        SPUtils.getInstance().put("rebate", rebate);
    }

    public float getRebate() {
        return SPUtils.getInstance().getFloat("rebate", 0f);
    }

    public void logout() {
        setToken("");
        setUser(null);
        App.getInstance().initRetrofit();
    }

    public float getBalance() {
        return SPUtils.getInstance().getFloat("wallet_balance", 0);
    }

    public void setBalance(float balance) {
        SPUtils.getInstance().put("wallet_balance", balance);
    }

    public void setUser(User user) {
        if (user != null) {
            setId(user.id);
            setUserId(user.user_id);
            setUserType(user.user_type);
            setRebate(user.rebate);
            SPUtils.getInstance().put("user_detail", App.getGson().toJson(user));
        } else {
            setId(0);
            setUserId("");
            setUserType(0);
            setRebate(0);
            SPUtils.getInstance().put("user_detail", "");
        }
    }

    public User getUser() {
        User user = null;
        String json = SPUtils.getInstance().getString("user_detail");
        if (!TextUtils.isEmpty(json)) {
            user = App.getGson().fromJson(json, User.class);
        }
        return user;
    }

    public String getIMToken() {
        return SPUtils.getInstance().getString("imtoken");
    }

    public void setIMToken(String imToken) {
        SPUtils.getInstance().put("imtoken", imToken);
    }

    public List<Integer> getLotteryBetButtons() {
        String btns = SPUtils.getInstance().getString("lottery_bet_buttons", Config.LOTTERY_BET_BUTTONS);
        if (TextUtils.isEmpty(btns)) {
            btns = Config.LOTTERY_BET_BUTTONS;
        }
        return App.getGson().fromJson(btns, new TypeToken<List<Integer>>() {
        }.getType());
    }

    public void setWin(float win) {
        SPUtils.getInstance().put("win", win);
    }

    public float getWin() {
        return SPUtils.getInstance().getFloat("win", 0);
    }

    public String getQrcode() {
        return SPUtils.getInstance().getString("qrcode", "");
    }

    public void setQrcode(String url) {
        SPUtils.getInstance().put("qrcode", url);
    }

    public void setLock(float lock) {
        SPUtils.getInstance().put("withdraw_qrcode", lock);
    }

    public float getLock() {
        return SPUtils.getInstance().getFloat("withdraw_qrcode", 0);
    }

    public void setWallet(Wallet wallet) {
        setBalance(wallet.balance);
        setWithdrawMinMoney(wallet.withdraw_min_money);
        setWin(wallet.win);
        setQrcode(wallet.qrcode);
        setLock(wallet.lock_money);
        setTodayWinMoney(wallet.today_win_money);
        setWalletPwdExist(wallet.pwd);
    }

    public void setWalletPwdExist(boolean exist) {
        SPUtils.getInstance().put("wallet_pwd", exist);
    }

    public boolean isWalletPwdExist() {
        return SPUtils.getInstance().getBoolean("wallet_pwd", false);
    }

    private void setWithdrawMinMoney(int money) {
        SPUtils.getInstance().put("withdraw_min_money", money);
    }

    public int getWithdrawMinMoney() {
        return SPUtils.getInstance().getInt("withdraw_min_money", 20);
    }

    private void setTodayWinMoney(float money) {
        SPUtils.getInstance().put("today_win_money", money);
    }

    public float getTodayWinMoney() {
        return SPUtils.getInstance().getFloat("today_win_money", 0);
    }

}
